package apple.encryptedNode.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import apple.encryptedNode.vo.Node;

public class OperationDAO {
	private EncryptedNodeOpenHelper dbHelper;
	private SQLiteDatabase db;
	private String tag=OperationDAO.class.getName();
	public OperationDAO(Context context) {
		dbHelper = new EncryptedNodeOpenHelper(context);
		
	}

	public Cursor query() {
		Log.d("OperationDAO", "query");
		db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				null, null, null, null, null, null);
		db.close();
		return cursor;

	}
	public ArrayList<Node> list() {
		Log.d("OperationDAO", "query");
		ArrayList<Node> list = new ArrayList<Node>();
		db = dbHelper.getWritableDatabase();
		String[] columns = new String[] { "KEY_NAME", "KEY_CONTENT",EncryptedNodeOpenHelper.KEY_ENCRYPT_CONTENT};
		Cursor cursor = db.query(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				columns, null, null, null, null, null);
		while (cursor.moveToNext()) {
			Node node=new Node();
			node.setTopicName(cursor.getString(0));
			node.setTopicContent(cursor.getString(1));
			list.add(node);
		}
		cursor.close();
		db.close();
		return list;

	}
	public long save(String fileName, String fileContent) {
		Log.d("OperationDAO", "save");
		db = dbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(EncryptedNodeOpenHelper.KEY_NAME, fileName);
		cv.put(EncryptedNodeOpenHelper.KEY_CONTENT, fileContent);
		
		
		long result = db.insert(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				null, cv);
		db.close();
		return result;

	}
	
	public long save(String fileName, String fileContent,String  value,String status) {
		Log.d("OperationDAO", "save");
		db = dbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(EncryptedNodeOpenHelper.KEY_NAME, fileName);
		cv.put(EncryptedNodeOpenHelper.KEY_CONTENT, fileContent);
		cv.put(EncryptedNodeOpenHelper.KEY_ENCRYPT_CONTENT, value);
		cv.put(EncryptedNodeOpenHelper.KEY_STATUS, status);
		long result = db.insert(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				null, cv);
		db.close();
		return result;

	}

	public void update(int id, String fileName, String fileContent) {
		Log.d("OperationDAO", "update");
		 db = dbHelper.getWritableDatabase();

		// String where = EncryptedNodeOpenHelper.KEY_NAME + "= ?";
		String where = "_id = ?";
		String[] wherevalues = { id + "" };

		ContentValues cv = new ContentValues();
		cv.put(EncryptedNodeOpenHelper.KEY_NAME, fileName);
		cv.put(EncryptedNodeOpenHelper.KEY_CONTENT, fileContent);

		db.update(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME, cv, where,
				wherevalues);
		db.close();

	}
	/***
	 *更新
	 *
	 * **/
	public void updateStatus(int id, String fileContent,String jiamivalue,String status) {
		Log.d("OperationDAO", "update");
		 db = dbHelper.getWritableDatabase();

		// String where = EncryptedNodeOpenHelper.KEY_NAME + "= ?";
		String where = "_id = ?";
		String[] wherevalues = { id + "" };

		ContentValues cv = new ContentValues();
		cv.put(EncryptedNodeOpenHelper.KEY_STATUS, status);
		cv.put(EncryptedNodeOpenHelper.KEY_CONTENT, fileContent);
		cv.put(EncryptedNodeOpenHelper.KEY_ENCRYPT_CONTENT, jiamivalue);
		
		db.update(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME, cv, where,
				wherevalues);
		db.close();

	}
	/***
	 * 删除
	 * **/
	public void delete(int id) {
		db = dbHelper.getWritableDatabase();
		String where = " _id = ?";
		String[] wherevalues = { id + "" };
		db.delete(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME, where,
				wherevalues);
		db.close();
	}
	/***
	 * 读取所有
	 * **/
	public ArrayList<Node> getList() {
		Log.d(tag, "getList");
		ArrayList<Node> list=new ArrayList<Node>();
		db = dbHelper.getWritableDatabase();
		String[] columns = new String[] {"_id", "fileName", "fileContent", "fileStatus","encryptfileContent"};
		Cursor cursor = db.query(EncryptedNodeOpenHelper.DICTIONARY_TABLE_NAME,
				columns, null, null, null, null, null);
		while (cursor.moveToNext()) {
			Node n=new Node();
			n.setId(cursor.getInt(0));
			n.setTopicName(cursor.getString(1));
			n.setTopicContent(cursor.getString(2));
			n.setEncryptionStatus(cursor.getString(3));
			n.setEncryptfileContent(cursor.getString(4));
			
			list.add(n);
		}
		Log.d(tag, "list size:"+list.size());
		cursor.close();
		db.close();
		return list;
	}
}
